package com.thread.lock;

import org.junit.Test;

import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @program: leetcode
 * @description:
 * @author: wangzhihua
 * @date: 2024-06-10
 */
public class TestLock {

	@Test
	public void test1() {

		AtomicInteger n = new AtomicInteger();
		Lock lock0 = new ReentrantLock();
		Lock lock1 = new ReentrantLock();

		new Thread(() -> {
			while (true) {
				lock0.lock();
				System.out.println(Thread.currentThread().getName() + ", n=" + (n.incrementAndGet()));
				lock1.unlock();
			}
		}, "thread--0").start();

		new Thread(() -> {
			while (true) {
				lock1.lock();
				System.out.println(Thread.currentThread().getName() + ", n=" + (n.incrementAndGet()));
				lock0.unlock();
			}
		}, "thread--1").start();
	}

}
